/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import domain.model.rms.UsuarioConfig;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import util.J2meAppHelper;

/**
 * 
 * @author ADM
 */
public class OperadoraForm implements AppForm, CommandListener {

    private Form form = new Form("Operadora");
    private TextField txCod = new TextField("Código", null, 2, TextField.NUMERIC);
    private StringItem exemplo = new StringItem("", "");
    private Command cmdVoltar = new Command("Voltar", Command.BACK, 0);
    private Command cmdOk = new Command("Salvar", Command.ITEM, 1);
    private ChoiceGroup choiceGroup = new ChoiceGroup("Usar interurbano", ChoiceGroup.EXCLUSIVE);
    private Form backForm = null;
    private int SIM_INDEX;
    private int NAO_INDEX;

    public OperadoraForm() {

        SIM_INDEX = choiceGroup.append("Sim", null);
        NAO_INDEX = choiceGroup.append("Não", null);

        form.append(choiceGroup);
        form.append(txCod);

        form.append(exemplo);


        form.addCommand(cmdVoltar);
        form.addCommand(cmdOk);
        form.setCommandListener(this);
    }

    public Displayable getDisplyable() {
        return form;
    }

    public void commandAction(Command arg0, Displayable arg1) {

        if (arg0.equals(cmdVoltar)) {
            if (backForm == null) {
                J2meAppHelper.getInstance().changeForm(InicialForm.getInicialForm());
                return;
            } else {
                J2meAppHelper.getInstance().getDisplay().setCurrent(backForm);
                return;
            }
        }

        if (arg0.equals(cmdOk)) {

            UsuarioConfig uc = J2meAppHelper.getInstance().getUc();

            uc.setUsarInterurbano(new Boolean(false));
            if (choiceGroup.getSelectedIndex() == SIM_INDEX) {
                uc.setUsarInterurbano(new Boolean(true));
            }

            uc.setOperadora(txCod.getString());
            Alert a = new Alert("Aviso", "Opearadora " + txCod.getString() + " salva com sucesso. " +
                    "Próximo numero será discado como 0" + txCod.getString() + "XX XXXX-XXX",
                    null, AlertType.INFO);
            uc.save();
            if (backForm == null) {
                J2meAppHelper.getInstance().changeForm(InicialForm.getInicialForm());
                return;
            } else {
                J2meAppHelper.getInstance().getDisplay().setCurrent(backForm);
                return;
            }
        }

    }

    public void initialize(Object parameter) {

        if (parameter instanceof Form) {
            backForm = (Form) parameter;
        } else {
            backForm = null;
        }

        UsuarioConfig uc = J2meAppHelper.getInstance().getUc();

        choiceGroup.setSelectedIndex(SIM_INDEX, false);
        choiceGroup.setSelectedIndex(NAO_INDEX, false);

        if (uc.getUsarInterurbano().booleanValue()) {
            choiceGroup.setSelectedIndex(SIM_INDEX, true);
        } else {
            choiceGroup.setSelectedIndex(NAO_INDEX, true);
        }

        txCod.setString(uc.getOperadora());

//        Integer t = cid.getDdd();
//        int ddd = t.intValue();
//        if (ddd != -1) {
        exemplo.setText("0__  XX XXXX-XXXX");
//        } else {
//            exemplo.setText("0__ " + "XX" + " XXXX-XXXX");
//        }

    }
}
